package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Admin;
import com.xmy.cultivate.service.IAdminService;
import com.xmy.cultivate.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty;
import static java.lang.Integer.parseInt;

/**
 * <p>
 * 登录账号 前端控制器
 * </p>
 *
 * @author min
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/api/admin")
@Scope("prototype")
public class AdminController {

    @Autowired
    IAdminService iAdminService;

    @PostMapping("index")
    public PageResult<Admin> index(@RequestBody Map reMap){
        int pageNum = isEmpty(reMap.get("pageNum").toString()) ? 1 : parseInt(reMap.get("pageNum").toString());
        int pageSize = isEmpty(reMap.get("pageSize").toString()) ? 1000: parseInt(reMap.get("pageSize").toString());
        IPage<Admin> iPage = new Page<>(pageNum,pageSize);
        String account = null;
        if (reMap.get("account") != null) {
            account = reMap.get("account").toString();
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(account),"account",account);
        iAdminService.page(iPage,queryWrapper);
        return new PageResult<>(iPage.getRecords(),iPage.getTotal(),iPage.getPages());
    }

}
